﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.IO;

using System.Web.Services;
using Newtonsoft.Json; 

namespace SGCP_UI
{
    public partial class wfr_ConsultarProveedor : System.Web.UI.Page, ICallbackEventHandler
    {
        localhost.Service obj;

        protected void Page_Load(object sender, EventArgs e)
        {
            obj = new localhost.Service();

            String cbReference =
              Page.ClientScript.GetCallbackEventReference(this,
              "arg", "mostrarResultados", "context");

            String callbackScript;
            callbackScript = "function llamarServidor(arg, context)" +
                "{" + cbReference + ";}";
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
                "llamarServidor", callbackScript, true);
            //ddlDistrito.DataSource = obj.getDistritos(); //implementar
            //ddlDistrito.DataBind();
        }

        private string _callbackArgument;

        public string GetCallbackResult()
        {
            return _callbackArgument;
        }

        public void RaiseCallbackEvent(string eventArgument)
        {
            int n = Convert.ToInt32(eventArgument.Split('-')[1]);
            gvResBusqueda.DataSource = BuscarProveedor(eventArgument.Split('-')[0]);
            gvResBusqueda.DataBind();
            
            StringWriter sr = new StringWriter();
            HtmlTextWriter htm = new HtmlTextWriter(sr);
            gvResBusqueda.RenderControl(htm);
            htm.Flush();
            _callbackArgument = sr.ToString();
        }

        public DataSet BuscarProveedor(String busca)
        {
            localhost.dtoProveedor odtoProveedor = new localhost.dtoProveedor();
            odtoProveedor.id_proveedor = Convert.ToInt32(busca);

            return obj.ObtenerProveedor(odtoProveedor);
        }

        [WebMethod]
        public static String getProveedor(Int32 idProveedor)
        {
            localhost.dtoProveedor odtoProveedor = new localhost.dtoProveedor();
            odtoProveedor.id_proveedor = idProveedor;

            //return new localhost.Service().ObtenerProveedor(odtoProveedor).Tables[0].Rows[0].ItemArray[3].ToString();            
            //return obj.ObtenerProveedor(odtoProveedor); 

            return JsonConvert.SerializeObject(new localhost.Service().ObtenerProveedor(odtoProveedor));
        }
    }
}